import { handleActions } from 'redux-actions'
import { PRACTICE_PUT, GET_HISTORY, ET_HISTORY_DETAIL } from '../types'

const getinitialLoginState = () => ({
  put: {},
  history: {},
  historyDetailId: 0
})

const initialState = getinitialLoginState()

const reducer = {
  [ET_HISTORY_DETAIL]: (state, action) => {
    return {
      ...state,
      historyDetailId: action.payload
    }
  },
  [GET_HISTORY]: (state, action) => {
    let historyMap = {}
    const { result } = action.payload
    if (Array.isArray(result.results)) {
      result.results.map(item => {
        historyMap[item.id] = item
      })
    }
    return {
      ...state,
      history: {
        ...state.history,
        ...action.payload,
        historyMap
      }
    }
  },
  [PRACTICE_PUT]: (state, action) => {
    return {
      ...state,
      put: {
        ...state.put,
        ...action.payload
      }
    }
  }
}

export default handleActions(
  reducer,
  initialState
)
